package com.hurl.waguji.web.admin;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.json.JSONUtil;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.hurl.library.util.StringUtils;
import com.hurl.waguji.base.SqlReader;
import com.hurl.waguji.base.dao.HibernateDao;
import com.hurl.waguji.base.dao.HibernateService;
import com.hurl.waguji.persistence.DzcxBO;
import com.hurl.waguji.persistence.DzcxColumnBO;
import com.hurl.waguji.web.extbo.GridData;
import com.opensymphony.xwork2.ActionSupport;

public class DzcxAction  extends ActionSupport{
	private int start = 0;
	private int limit = 20;
	private GridData gridData = new GridData();
	private Integer result;
	
	private String bt;
	private String sm;
	private String renderer;
	private String sqlcode;
	private Character flag;
	private String columns;
	
	private String ids;
	
	private Integer id;
	private String editdata;
	
	public String getEditdata() {
		return editdata;
	}
	public void setEditdata(String editdata) {
		this.editdata = editdata;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public Character getFlag() {
		return flag;
	}
	public void setFlag(Character flag) {
		this.flag = flag;
	}
	public String getColumns() {
		return columns;
	}
	public void setColumns(String columns) {
		this.columns = columns;
	}
	public String getSqlcode() {
		return sqlcode;
	}
	public void setSqlcode(String sql) {
		this.sqlcode = sql;
	}
	public String getBt() {
		return bt;
	}
	public void setBt(String bt) {
		this.bt = bt;
	}
	public String getSm() {
		return sm;
	}
	public void setSm(String sm) {
		this.sm = sm;
	}
	public String getRenderer() {
		return renderer;
	}
	public void setRenderer(String renderer) {
		this.renderer = renderer;
	}
	public Integer getResult() {
		return result;
	}
	public void setResult(Integer result) {
		this.result = result;
	}
	
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public GridData getGridData(){
		return this.gridData;
	}
	public String delete()throws Exception{
		if(StringUtils.isNotBlank(ids)){
			List<Long> idlist = (List<Long>)JSONUtil.deserialize(this.ids);
			if(!idlist.isEmpty()){
				HibernateDao dao = HibernateService.getDao();
				for(int i=0;i<idlist.size();i++){
					DzcxBO bo = new DzcxBO();
					bo.setId(idlist.get(i).intValue());
					dao.delete(bo);
					dao.flush();
					dao.clear();
					
					List<DzcxColumnBO> bo2list = dao.createCriteria(DzcxColumnBO.class).add(Restrictions.eq("dzcxId", bo.getId())).list();
					for(DzcxColumnBO bo2 : bo2list){
						dao.delete(bo2);
					}
					dao.flush();
					dao.clear();
				}
				dao.flush();
				dao.clear();
			}
		}
		result = 1;
		return SUCCESS;
	}
	public String add() throws Exception{
		if(StringUtils.isNotBlank(this.bt) && StringUtils.isNotBlank(this.sqlcode) && StringUtils.isNotBlank(this.columns)){
			/** 保存定制查询BO */
			DzcxBO bo = new DzcxBO();
			bo.setBt(this.bt);
			bo.setSqlcode(this.sqlcode);
			bo.setSm(this.sm);
			bo.setFlag(this.flag);
			bo.setCtime(new Date());
			HibernateDao dao = HibernateService.getDao();
			dao.save(bo);
			dao.flush();
			Integer dzcxId = bo.getId();
			
			/** 保存列定义BO */
			List<Map<String,String>> columns = (List<Map<String,String>>)JSONUtil.deserialize(this.columns);
			List<DzcxColumnBO> list = new ArrayList<DzcxColumnBO>();
			for(int i=0;i<columns.size();i++){
				Map<String,String> column = columns.get(i);
				if(StringUtils.isNotBlank(column.get("mc"))){
					DzcxColumnBO bo2 = new DzcxColumnBO();
					bo2.setDm(StringUtils.isBlank(column.get("dm"))?"":column.get("dm"));
					bo2.setMc(StringUtils.isBlank(column.get("mc"))?"":column.get("mc"));
					bo2.setSjlx(StringUtils.isBlank(column.get("sjlx"))?"string":column.get("sjlx"));
					bo2.setWidth((StringUtils.isBlank(column.get("width"))||column.get("width").equals("0"))?150:Integer.valueOf(column.get("width")));
					bo2.setDzcxId(dzcxId);
					bo2.setIdx(StringUtils.isBlank(column.get("idx"))?0:Integer.valueOf(column.get("idx")));
					bo2.setRenderer(StringUtils.isBlank(column.get("renderer"))?"":column.get("renderer"));
					list.add(bo2);
				}
			}
			if(!list.isEmpty()){
				dao.saveList(list);
				dao.flush();
				dao.clear();
			}
			result = 1;
		}else{
			result = 0;
		}
		return SUCCESS;
	}
	public String list() throws Exception{
		this.gridData = new GridData();
		HibernateDao dao = HibernateService.getDao();
		
		/* 总数 */
		int total = ((BigInteger)dao.queryBySQL(SqlReader.getSql("SQL12101801")).list().get(0)).intValue();
		gridData.setTotalProperty(total);
		
		/* 具体数据 */
		List<DzcxBO> list = (List<DzcxBO>)dao.createCriteria(DzcxBO.class)
					.addOrder(Order.desc("ctime"))
					.setReadOnly(true)
					.setFirstResult(this.getStart())
					.setMaxResults(this.getLimit())
					.list();
		
		if(list!=null && list.size()>0){
			Map<String,Object> map = null;
			for(DzcxBO bo : list){
				map = new HashMap<String,Object>();
				map.put("id", bo.getId());
				map.put("bt", bo.getBt());
				map.put("flag", bo.getFlag());
				gridData.getRoot().add(map);
			}
		}
		dao.clear();
		return SUCCESS;
	}
	public String edit()throws Exception{
		if(this.id!=null){
			HibernateDao dao = HibernateService.getDao();
			DzcxBO bo = dao.get(DzcxBO.class, this.id);
			if(bo!=null){
				Map<String,Object> map = new HashMap<String,Object>(2);
				map.put("result", 1);
				map.put("dzcx", bo);

				List<DzcxColumnBO> bo2 = (List<DzcxColumnBO>)dao.createCriteria(DzcxColumnBO.class).add(Restrictions.eq("dzcxId", this.id)).addOrder(Order.asc("idx")).list();
				map.put("dzcx_column", bo2);
				
				editdata = JSONUtil.serialize(map);
			}
			dao.flush();
			dao.clear();
		}else{
			Map<String,Object> map = new HashMap<String,Object>(2);
			map.put("result", 0);
			editdata = JSONUtil.serialize(map);
		}
		return SUCCESS;
	}
	public String saveedit()throws Exception{
		HibernateDao dao = HibernateService.getDao();
		
		/** 更新DzcxBO */
		DzcxBO bo = dao.load(DzcxBO.class, this.id);
		bo.setBt(this.bt);
		bo.setSqlcode(this.sqlcode);
		bo.setSm(this.sm);
		bo.setFlag(this.flag);
		bo.setCtime(new Date());
		dao.update(bo);
		dao.flush();
		dao.clear();
		
		/** 更新DzcxColumnBO */
		/* 先删除 */
		List<DzcxColumnBO> bo2list = dao.createCriteria(DzcxColumnBO.class).add(Restrictions.eq("dzcxId", bo.getId())).list();
		for(DzcxColumnBO bo2 : bo2list){
			dao.delete(bo2);
		}
		dao.flush();
		dao.clear();
		/* 后添加 */
		List<Map<String,String>> columns = (List<Map<String,String>>)JSONUtil.deserialize(this.columns);
		List<DzcxColumnBO> list = new ArrayList<DzcxColumnBO>();
		for(int i=0;i<columns.size();i++){
			Map<String,String> column = columns.get(i);
			if(StringUtils.isNotBlank(column.get("mc"))){
				DzcxColumnBO bo2 = new DzcxColumnBO();
				bo2.setDm(StringUtils.isBlank(column.get("dm"))?"":column.get("dm"));
				bo2.setMc(StringUtils.isBlank(column.get("mc"))?"":column.get("mc"));
				bo2.setSjlx(StringUtils.isBlank(column.get("sjlx"))?"string":column.get("sjlx"));
				bo2.setWidth((StringUtils.isBlank(column.get("width"))||column.get("width").equals("0"))?150:Integer.valueOf(column.get("width")));
				bo2.setDzcxId(this.id);
				bo2.setRenderer(StringUtils.isBlank(column.get("renderer"))?"":column.get("renderer"));
				bo2.setIdx(StringUtils.isBlank(column.get("idx"))?0:Integer.valueOf(column.get("idx")));
				list.add(bo2);
			}
		}
		if(!list.isEmpty()){
			dao.saveList(list);
			dao.flush();
			dao.clear();
		}
		result = 1;
		return SUCCESS;
	}
}
